package ${package}.app.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        List<Parameter> pars = new ArrayList<Parameter>() {{
            add(new ParameterBuilder()
                    .name("X-CYBER-Token")
                    .description("用户的token值")
                    .modelRef(new ModelRef("string"))
                    .parameterType("header")
                    .required(false)
                    .build()
            );
        }};

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //加了ApiOperation注解的类，才生成接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //包下的类，才生成接口文档
                //.apis(RequestHandlerSelectors.basePackage("io.renren"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(security())
                .globalOperationParameters(pars)
                .globalResponseMessage(RequestMethod.GET, responseMessages)
                .globalResponseMessage(RequestMethod.POST, responseMessages)
                .globalResponseMessage(RequestMethod.PUT, responseMessages);
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("cyber.pan", "", "panyiboy@gmail.com");
        StringBuffer sb = new StringBuffer(1024);
        sb.append("<strong>注意事项:</strong><ul>");
        sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<li>1.本接口文档,如果没有特殊限定,默认仅支持<strong>POST</strong>方式。<br></li>");
        sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<li>2.参数描述<strong>模型</strong>和<strong>示例</strong>,分别针对请求的<strong>参数结构</strong>和<strong>参数示例</strong>进行描述。<br></li>");
        sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<li>3.响应描述<strong>模型</strong>和<strong>示例</strong>,分别针对响应的<strong>数据结构</strong>和<strong>数据示例</strong>进行描述。<br></li></ul>");
        return new ApiInfoBuilder()
                .title("")
                .description(sb.toString())
                .contact(contact)
                .version("1.1.0")
                .build();
    }

    private ArrayList<ResponseMessage> responseMessages = new ArrayList<ResponseMessage>() {
        private static final long serialVersionUID = 1L;

        {
            add(new ResponseMessageBuilder()
                    .code(0)
                    .message("success")
                    .build()
            );
            add(new ResponseMessageBuilder()
                    .code(900)
                    .message("无效的token值")
                    .responseModel(new ModelRef("R"))
                    .build()
            );
            add(new ResponseMessageBuilder()
                    .code(500).message("系统异常")
                    .responseModel(new ModelRef("R"))
                    .build()
            );
            add(new ResponseMessageBuilder()
                    .code(404)
                    .message("资源不存在")
                    .responseModel(new ModelRef("R"))
                    .build()
            );
        }
    };

    private List<ApiKey> security() {
        return newArrayList(
                new ApiKey("token", "X-CYBER-Token", "header")
        );
    }

}